

function getVal(list){
    for(var i = 0; i < list.length; i++){
        if(list[i].checked){
            return list[i].value;
        }
    }
    return null;
}

function click_at(){
    var categoryTag = document.getElementById("category");
    var categoryVal = categoryTag.options[categoryTag.selectedIndex].value;

    var levelTag = document.getElementById("level");
    var levelVal = levelTag.options[levelTag.selectedIndex].value;

    var sizeTag = document.getElementById("size");
    var sizeVal = sizeTag.options[sizeTag.selectedIndex].value;

    var modeTag = document.getElementsByName("mode");
    var modeVal = getVal(modeTag);


    var timeTag = document.getElementById("countdown");
    var timeVal = timeTag.value;
    if(modeVal == 'time' && check_value(document.getElementById("countdown")) == false){
        return ;
    }

    var mode2Tags = document.getElementsByName("mode2");
    var mode2Val = getVal(mode2Tags);


    // if(categoryVal != null && levelVal != null && sizeVal != null){
    var link = "question.php?category="+categoryVal+"&level="+levelVal+"&size="+sizeVal+"&mode="+modeVal+"&mode2="+mode2Val;
    if(timeVal == '' || timeVal == null){
        window.location = link;
    }
    window.location = link+"&time="+timeVal;


}

function check_value(tag){
    var val = tag.value;
    if(val == "" || isNaN(val)){
        alert("please enter an integer")
        return false;
    }
    if(val < 5 || val > 20){
        alert("seconds should be in the interval [5,20]")
        return false;
    }

    return true;
}

window.onload=function (ev) {
    $(".mode").change(function () {
        if(this.value == 'normal'){
            var tag = document.getElementById("countdown");
            tag.value="";
            tag.setAttribute("readonly","readonly");
        }else{
            document.getElementById("countdown").removeAttribute("readonly");
        }
    })
}